package com.leetcode.search.dfs;

import com.leetcode.basic.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dennis Li
 * @date 2020/7/25 22:43
 */
public class BinaryTreePaths_257 {

    private List<String> ans;

    public List<String> binaryTreePaths(TreeNode root) {
        ans = new ArrayList<>();
        if (root == null) return ans;
        dfs(root, new StringBuilder());
        return ans;
    }

    private void dfs(TreeNode root, StringBuilder sb) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            ans.add(sb.append(root.val).toString());
            return;
        }
        StringBuilder builder = new StringBuilder(sb).append(root.val).append("->");
        dfs(root.left, builder);
        dfs(root.right, new StringBuilder(builder));
    }

}
